using System.Collections.Generic;
using UnityEngine;

public class ItemDrop : MonoBehaviour
{
    [SerializeField] private int possibleItemDrop;
    private GameObject prefab;
    [SerializeField] private ItemData[] possibleItems;
    private List<ItemData> dropList = new();

    void Awake()
    {
        prefab = Resources.Load<GameObject>("Prefabs/Item object - No data");
    }

    public virtual void GenerateDrop()
    {
        foreach (var item in possibleItems)
        {
            if (Random.Range(0, 100) <= item.DropChance)
            {
                dropList ??= new List<ItemData>();
                dropList.Add(item);
            }
        }
        for (int i = 0; i < possibleItemDrop; i++)
        {
            ItemData randomItem = possibleItems[Random.Range(0, dropList.Count)];
            _ = dropList.Remove(randomItem);
            DropItem(randomItem);
        }
    }

    protected void DropItem(ItemData item)
    {
        GameObject newDrop = Instantiate(prefab, transform.position, Quaternion.identity);
        Vector2 randomVelocity = new Vector2(Random.Range(-5f, 5f), Random.Range(15f, 20f));
        newDrop.GetComponent<ItemObject>().SetupItem(item, randomVelocity);
    }
}
